# Build Options
export ARCH := aarch64
export LOG := info
export BACKTRACE := y
export MEMTRACK := n
export MODE := release

# QEMU Options
export BLK := y
export NET := y
export MEM := 1G
export ICOUNT := n

# Generated Options
export A := $(PWD)
export NO_AXSTD := y
export AX_LIB := axfeat
export APP_FEATURES := qemu

ifeq ($(MEMTRACK), y)
	APP_FEATURES += starry-api/memtrack
endif

IMG_URL = https://github.com/Starry-OS/rootfs/releases/download/20250917
IMG = rootfs-$(ARCH).img
APP_NAME := $(notdir $(CURDIR))

img:
	@if [ ! -f $(IMG) ]; then \
		echo "Image not found, downloading..."; \
		curl -f -L $(IMG_URL)/$(IMG).xz -O; \
		xz -d $(IMG).xz; \
	fi
	@cp $(IMG) arceos/disk.img

defconfig justrun clean:
	@make -C arceos $@

build run debug: defconfig
	@make -C arceos $@

# Aliases
rv:
	$(MAKE) ARCH=riscv64 run

la:
	$(MAKE) ARCH=loongarch64 run

vf2:
	$(MAKE) ARCH=riscv64 APP_FEATURES=vf2 MYPLAT=axplat-riscv64-visionfive2 BUS=dummy build

2k1000la:
	$(MAKE) ARCH=loongarch64 APP_FEATURES=2k1000la MYPLAT=axplat-loongarch64-2k1000la BUS=dummy build

qemu:
	$(MAKE) ARCH=aarch64 APP_FEATURES=qemu FEATURES="driver-ramdisk,fat" DEBUG=y run

opi-aipro:
	$(MAKE) ARCH=aarch64 APP_FEATURES=opi-aipro MYPLAT=axplat-aarch64-opi-aipro BUS=dummy FEATURES="driver-ramdisk,fat" build
	@scripts/Ascend310B/build.sh starryos

sd: 
	sudo dd if=$(APP_NAME).bin of=/dev/sdx bs=512 seek=32768 status=progress
	@sync

disasm:
	rust-objdump --arch=aarch64 -SD $(APP_NAME)_aarch64-opi-aipro.elf > $(APP_NAME)_aarch64-opi-aipro.asm

.PHONY: build run justrun debug disasm clean sd
